package com.fdfs.api.serviceimpl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;

import org.apache.thrift.TException;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fdfs.api.service.FdfsApiService.Iface;

public class FdfsApiServiceImpl implements Iface {
	private static final Logger log = LoggerFactory.getLogger(FdfsApiServiceImpl.class);
	@Override
	public String upload_file(ByteBuffer local_file,String ext) throws TException {
		String fileId = "";
		TrackerServer trackerServer = null;
		try {
			
			TrackerClient tracker = new TrackerClient();
			trackerServer = tracker.getConnection();
			StorageServer storageServer = null;
//			storageServer = tracker.getStoreStorage(trackerServer,"group2");
			StorageClient1 storageClient = new StorageClient1(trackerServer,
					storageServer);
			fileId = storageClient.upload_file1(local_file.array(), ext,
					null);

			log.info(" 路径: " + fileId);

			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		} finally  {
			try {
				trackerServer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return fileId;
	}

}
